﻿
var mac_Devices = new Array();
var mac_Interfaces = new Array();
var mac_I = new Array();
var mac_O = new Array();
var mac_ID = new Array();
var mac_refresh = true;

var mac_I_last = 0;
var mac_O_last = 0;

function GenerateDeviceList() {
	var tmp;
	var i;

	//var tmpdoc = XMLDOM.parse( runCommand("type list1.xml") );
	var tmpdoc = XMLDOM.parse( runCommand("system_profiler -xml SPNetworkDataType") );
	var tmpList = tmpdoc.evaluate( "/plist/array[1]/dict[1]/array[1]/dict" );
	var tmpNode;

	var tmpIF;
	var tmpName;

	for(i=0;i<tmpList.length;i++) {
		tmpNode = tmpList.item(i).childNodes;
		tmpIF = "";
		tmpName = "";
		for(j=0;j<tmpNode.length;j++) {
			if( tmpNode.item(j).firstChild.data == "interface" ) tmpIF = tmpNode.item(j + 1).firstChild.data;
			if( tmpNode.item(j).firstChild.data == "_name" )     tmpName = tmpNode.item(j + 1).firstChild.data;
		}
		if(tmpIF != "" && tmpName != "") mac_Devices[tmpIF] = tmpName;
	}


	//var mac_GetInterfaces = runCommand("type list2.txt");
	var mac_GetInterfaces = runCommand("netstat -i");
	mac_GetInterfaces = mac_GetInterfaces.split("\n");
	for(i=1;i<mac_GetInterfaces.length;i++) {
		tmp = mac_GetInterfaces[i].replace(/  /g, " ");
		while(tmp.lastIndexOf("  ")>-1)
			tmp = tmp.replace(/  /g, " ");
		tmp = tmp.split(" ");
		if(tmp[4]>0) mac_Interfaces[tmp[0]] = parseInt(tmp[1]); // MTU
//		if(tmp[3]=="localhost")
//			net_NAME[0]=tmp[0];
	}


	for(i in mac_Interfaces) {
		if(mac_Devices[i]) {
			mac_ID[net_NAME.length] = i;
			net_NAME[net_NAME.length]=mac_Devices[i];
			if(net_USED == -1) net_USED = net_NAME.length - 1;
		}
	}
	if(net_USED == -1) net_USED = 0;
	if(!mac_ID[net_USED])net_USED=0;

	for(i in net_NAME)
		print("Devices ["+i+"] = ["+net_NAME[i]+"]");

	print("Active Devices ["+net_USED+"] = ["+net_NAME[net_USED]+"]");


	return true;
}

function GetDeviceIN() {
	if(mac_refresh==true) GetIO();
	else mac_refresh=true;

	if(mac_I_last==0) mac_I_last = mac_I[mac_ID[net_USED]];
	tmp = mac_I[mac_ID[net_USED]] - mac_I_last;
	mac_I_last = mac_I[mac_ID[net_USED]];

	return tmp/mod_netmonitor_timer.interval;
}

function GetDeviceOUT() {
	if(mac_refresh==true) GetIO();
	else mac_refresh=true;

	if(mac_O_last==0) mac_O_last = mac_O[mac_ID[net_USED]];
	tmp = mac_O[mac_ID[net_USED]] - mac_O_last;
	mac_O_last = mac_O[mac_ID[net_USED]];

	return tmp/mod_netmonitor_timer.interval;
}

function GetDeviceBW() {
	//Returns the available Connection Bandwidth (e.g. FastEthernet = 100000000 bytes per second)
	return 100000000;
}

function GetIO() {
	//var mac_GetInterfaces = runCommand("type list2.txt");
	var mac_GetInterfaces = runCommand("netstat -I " + mac_ID[net_USED] + " -b");
	mac_GetInterfaces = mac_GetInterfaces.split("\n");

	for(var i=1;i<mac_GetInterfaces.length;i++) {
		tmp = mac_GetInterfaces[i].replace(/  /g, " ");
		while(tmp.lastIndexOf("  ")>-1)
			tmp = tmp.replace(/  /g, " ");
		tmp = tmp.split(" ");
		mac_I[tmp[0]] = parseInt(tmp[6]);
		mac_O[tmp[0]] = parseInt(tmp[9]);
	}
	mac_refresh=false;
	return true;
}
